part of '../../repository.dart';

const _kYookassaPaymentMethodIdKey = 'yookassa_api_payment_method_id';

const _kWebPaymentMethodPaymentSystemValue = 'yookassa';

class _YookassaPaymentMethodDto extends Equatable implements _WebPaymentMethodDto {
  const _YookassaPaymentMethodDto._internal({
    required this.id,
    required this.userId,
    required this.saved,
    required this.test,
    required this.bankCardInfoDto,
    required this.failureDto,
    required this.yookassaApiPaymentMethodId,
  });

  @override
  final String id;

  @override
  final String userId;

  @override
  final bool saved;

  @override
  final bool test;

  @override
  final _YookassaBankCardInfoDto bankCardInfoDto;

  @override
  final _WebPaymentMethodFailureDto? failureDto;

  final String? yookassaApiPaymentMethodId;

  @override
  String get paymentSystem => _kWebPaymentMethodPaymentSystemValue;

  @override
  List<Object?> get props {
    return [
      id,
      userId,
      saved,
      test,
      bankCardInfoDto,
      failureDto,
      yookassaApiPaymentMethodId,
    ];
  }

  factory _YookassaPaymentMethodDto.fromJson(Map<String, dynamic> json) {
    final yookassaBankCardInfoJson =
        json[_kWebPaymentMethodBankCardInfoKey] as Map<String, dynamic>;

    final bankCardInfoDto = _YookassaBankCardInfoDto.fromJson(yookassaBankCardInfoJson);

    final failureString = json[_kWebPaymentMethodFailureKey] as String?;

    _WebPaymentMethodFailureDto? failureDto;

    if (failureString != null) {
      failureDto = _WebPaymentMethodFailureDto.fromString(failureString);
    }

    return _YookassaPaymentMethodDto._internal(
      id: json[_kWebPaymentMethodIdKey],
      userId: json[_kWebPaymentMethodUserIdKey],
      saved: json[_kWebPaymentMethodSavedKey],
      test: json[_kWebPaymentMethodTestKey],
      bankCardInfoDto: bankCardInfoDto,
      failureDto: failureDto,
      yookassaApiPaymentMethodId: json[_kYookassaPaymentMethodIdKey],
    );
  }

  factory _YookassaPaymentMethodDto.fromEntity(YookassaPaymentMethod paymentMethod) {
    final bankCardInfoDto = _YookassaBankCardInfoDto.fromEntity(paymentMethod.bankCardInfo);

    _WebPaymentMethodFailureDto? failureDto;

    if (paymentMethod.failure != null) {
      failureDto = _WebPaymentMethodFailureDto.fromEntity(paymentMethod.failure!);
    }

    return _YookassaPaymentMethodDto._internal(
      id: paymentMethod.id,
      userId: paymentMethod.userId,
      saved: paymentMethod.saved,
      test: paymentMethod.test,
      yookassaApiPaymentMethodId: paymentMethod.yookassaApiPaymentMethodId,
      bankCardInfoDto: bankCardInfoDto,
      failureDto: failureDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kWebPaymentMethodIdKey: id,
      _kWebPaymentMethodUserIdKey: userId,
      _kWebPaymentMethodSavedKey: saved,
      _kWebPaymentMethodTestKey: test,
      _kWebPaymentMethodPaymentSystemKey: paymentSystem,
      _kYookassaPaymentMethodIdKey: yookassaApiPaymentMethodId,
      _kWebPaymentMethodFailureKey: failureDto?.toString(),
      _kWebPaymentMethodBankCardInfoKey: bankCardInfoDto.toJson(),
    };
  }

  @override
  YookassaPaymentMethod toEntity() {
    return YookassaPaymentMethod(
      id: id,
      userId: userId,
      saved: saved,
      test: test,
      yookassaApiPaymentMethodId: yookassaApiPaymentMethodId,
      bankCardInfo: bankCardInfoDto.toEntity(),
      failure: failureDto?.toEntity(),
    );
  }
}
